using System;
using System.Collections;
using UnityEngine;

namespace M13.Base
{
    /// <summary>
    /// ��Դ���ذ�����
    /// </summary>
    public static class ResHelper
    {
        /// <summary>
        /// ͬ��������Դ
        /// </summary>
        public static t SyncLoad<t>(string name) where t : UnityEngine.Object
        {
            t res = Resources.Load<t>(name);
            return res is GameObject ? GameObject.Instantiate(res) : res;
        }

        /// <summary>
        /// �첽������Դ
        /// </summary>
        public static void AsyncLoad<t>(string name, Action<t> callback) where t : UnityEngine.Object =>
            PublicMono.GetInstance().StartCoroutine(AsyncLoadRes(name, callback));

        /// <summary>
        /// �첽����Э�̺���
        /// </summary>
        private static IEnumerator AsyncLoadRes<t>(string name, Action<t> callback) where t : UnityEngine.Object
        {
            var r = Resources.LoadAsync<t>(name);
            while (!r.isDone) yield return null;
            callback(r.asset is GameObject ? GameObject.Instantiate(r.asset) as t : r.asset as t);
        }

        /// <summary>
        /// ͬ��������Դ����
        /// </summary>
        public static t[] SyncLoadAll<t>(string name) where t : UnityEngine.Object => Resources.LoadAll<t>(name);
    }
}